package com.talkyun.openx.schema;

import com.talkyun.openx.ocean.RegistryAware;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.w3c.dom.Element;

class OpenxRegistryBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
    private boolean parsed = false;

    @Override
    protected boolean shouldGenerateId() {
        return true;
    }

    @Override
    protected Class getBeanClass(Element element) {
        return RegistryAware.class;
    }

    @Override
    protected void doParse(Element element, BeanDefinitionBuilder bean) {
        if (parsed) {
            throw new RuntimeException("Registry is already defined!");
        }

        String url = element.getAttribute("url");
        if (url == null || url.trim().length() == 0) {
            throw new RuntimeException("Registry url is null!");
        }
        bean.addPropertyValue("url", url);
        parsed = true;
    }
}